<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>最后的修改 | Elasticsearch: 权威指南 | Elastic</title>
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
</head>
<body>
<div class="main-container">
    <section id="content">
        
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原文地址: <a href="https://www.elastic.co/guide/cn/elasticsearch/guide/current/_one_final_modification.html" rel="nofollow">https://www.elastic.co/guide/cn/elasticsearch/guide/current/_one_final_modification.html</a>, 版权归 www.elastic.co 所有<br/>
                            英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/_one_final_modification.html" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/guide/current/_one_final_modification.html</a>
                            </div>
                        <!-- start body -->
                  <div class="page_header">
<b>请注意:</b><br>本书基于 Elasticsearch 2.x 版本，有些内容可能已经过时。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch: 权威指南</a></span>
»
<span class="breadcrumb-link"><a href="aggregations.html">聚合</a></span>
»
<span class="breadcrumb-link"><a href="_aggregation_test_drive.html">尝试聚合</a></span>
»
<span class="breadcrumb-node">最后的修改</span>
</div>
<div class="navheader">
<span class="prev">
<a href="_buckets_inside_buckets.html">« 嵌套桶</a>
</span>
<span class="next">
<a href="_building_bar_charts.html">条形图 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="_one_final_modification"></a>最后的修改<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/300_Aggregations/23_extra_metrics.asciidoc">edit</a>
</h2>
</div></div></div>
<p>让我们回到话题的原点，在进入新话题之前，对我们的示例做最后一个修改， 为每个汽车生产商计算最低和最高的价格：</p>
<div class="pre_wrapper lang-sense">
<pre class="programlisting prettyprint lang-sense">GET /cars/transactions/_search
{
   "size" : 0,
   "aggs": {
      "colors": {
         "terms": {
            "field": "color"
         },
         "aggs": {
            "avg_price": { "avg": { "field": "price" }
            },
            "make" : {
                "terms" : {
                    "field" : "make"
                },
                "aggs" : { <a id="CO190-1"></a><i class="conum" data-value="1"></i>
                    "min_price" : { "min": { "field": "price"} }, <a id="CO190-2"></a><i class="conum" data-value="2"></i>
                    "max_price" : { "max": { "field": "price"} } <a id="CO190-3"></a><i class="conum" data-value="3"></i>
                }
            }
         }
      }
   }
}</pre>
</div>
<div class="sense_widget" data-snippet="snippets/300_Aggregations/20_basic_example.json"></div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO190-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>我们需要增加另外一个嵌套的 <code class="literal">aggs</code> 层级。</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO190-2"><i class="conum" data-value="2"></i></a></p>
</td>
<td align="left" valign="top">
<p>然后包括 <code class="literal">min</code> 最小度量。</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO190-3"><i class="conum" data-value="3"></i></a></p>
</td>
<td align="left" valign="top">
<p>以及 <code class="literal">max</code> 最大度量。</p>
</td>
</tr>
</table>
</div>
<p> 得到以下输出（只显示部分结果）：</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
...
   "aggregations": {
      "colors": {
         "buckets": [
            {
               "key": "red",
               "doc_count": 4,
               "make": {
                  "buckets": [
                     {
                        "key": "honda",
                        "doc_count": 3,
                        "min_price": {
                           "value": 10000 <a id="CO191-1"></a><i class="conum" data-value="1"></i>
                        },
                        "max_price": {
                           "value": 20000 <a id="CO191-2"></a><i class="conum" data-value="1"></i>
                        }
                     },
                     {
                        "key": "bmw",
                        "doc_count": 1,
                        "min_price": {
                           "value": 80000
                        },
                        "max_price": {
                           "value": 80000
                        }
                     }
                  ]
               },
               "avg_price": {
                  "value": 32500
               }
            },
...</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO191-1"><i class="conum" data-value="1"></i></a><a href="#CO191-2"></a></p>
</td>
<td align="left" valign="top">
<p><code class="literal">min</code> 和 <code class="literal">max</code>  度量现在出现在每个汽车制造商（ <code class="word">make</code> ）下面。</p>
</td>
</tr>
</table>
</div>
<p>有了这两个桶，我们可以对查询的结果进行扩展并得到以下信息：</p>
<div class="ulist itemizedlist">
<ul class="itemizedlist">
<li class="listitem">
有四辆红色车。
</li>
<li class="listitem">
红色车的平均售价是 $32，500 美元。
</li>
<li class="listitem">
其中三辆红色车是 honda (本田制造)，一辆是 bmw (宝马制造)。
</li>
<li class="listitem">
最便宜的红色本田售价为 $10，000 美元。
</li>
<li class="listitem">
最贵的红色本田售价为 $20，000 美元。
</li>
</ul>
</div>
</div>
<div class="navfooter">
<span class="prev">
<a href="_buckets_inside_buckets.html">« 嵌套桶</a>
</span>
<span class="next">
<a href="_building_bar_charts.html">条形图 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>